//
//  ZYCLectureRecordStatisticsViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/23.
//

#import "ZYCLectureRecordStatisticsViewController.h"
#import "PullDownMenuView.h"
#import "ZYCLectureRecordStatisticsCell.h"
#import "ZYCPullDownMenuViewController.h"
#import "ZYCLectureRecordStatisticsModel.h"
#import "ZYCCommonBaseModel.h"
#import "ZYCTourStatisticsModel.h"
#import "ZYCNoDataView.h"
@interface ZYCLectureRecordStatisticsViewController ()<UITableViewDelegate,UITableViewDataSource,PullDownMenuViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) PullDownMenuView *menuView;
@property (nonatomic, strong) UIButton *statisticsButton;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *viewControlls;
@property (nonatomic, strong) NSArray *testArr;
@property (nonatomic, strong) ZYCNoDataView *noDataView;

@property (nonatomic, copy) NSString *semesterId;
@property (nonatomic, copy) NSString *currentSemesterName;
@end

@implementation ZYCLectureRecordStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"统计";
    ZYCCommonBaseModel *model = self.testArr.firstObject;
    self.semesterId = model.nameId;
    self.currentSemesterName = model.name;
    
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.tableView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.statisticsButton];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealWithNot:) name:UpdateMenuTitleNote object:nil];
    [self loadLectureRecordStatistics];
}

- (void)dealWithNot:(NSNotification *)note {
    if (![self.viewControlls containsObject:note.object]) {
        return;
    }
    NSInteger col = [self.viewControlls indexOfObject:note.object];
    NSArray *allValues = note.userInfo.allValues;
//    // 不需要设置标题,字典个数大于1，或者有数组
    if (allValues.count > 1 || [allValues.firstObject isKindOfClass:[NSArray class]]) return ;
    if (col == 0) {
        ZYCCommonBaseModel *model = allValues.firstObject;
        self.semesterId = model.nameId;
    }
    [self loadLectureRecordStatistics];
}

- (void)loadLectureRecordStatistics{
    NSDictionary *params = @{
        @"semesterId":self.semesterId ? self.semesterId : @""
    };
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_queryAppCheckStaById requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        if (SUCCESS(responseObject[@"code"])) {
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCTourStatisticsModel class] json:responseObject[@"body"][@"checkList"]];
            NSString *checkCountSum = [NSString stringWithFormat:@"总计:%@次",responseObject[@"body"][@"checkCountSum"]];
            [self.statisticsButton setTitle:checkCountSum forState:UIControlStateNormal];
            [self.statisticsButton sizeToFit];
            
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:array];
            if (self.dataSource.count == 0) {
                self.noDataView.hidden = NO;
                self.tableView.hidden = YES;
            }else{
                self.noDataView.hidden = YES;
                self.tableView.hidden = NO;
            }
            [self.tableView reloadData];
            
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}


#pragma mark - getters
- (UIButton *)statisticsButton {
    if (!_statisticsButton) {
        _statisticsButton = [[UIButton alloc] init];
        _statisticsButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 15.0f);
    }
    return _statisticsButton;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCLectureRecordStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCLectureRecordStatisticsCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    [cell setSectionCircularTableView:tableView withIndexPath:indexPath];
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 280;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark - PullDownMenuViewDataSource
// 返回下拉菜单每列按钮标题
- (NSString *)pullDownMenu:(PullDownMenuView *)pullDownMenu titleForColAtIndex:(NSInteger)index {
    return [self.titleArr objectAtIndex:index];
}

// 返回下拉菜单每列对应的控制器
- (UIViewController *)pullDownMenu:(PullDownMenuView *)pullDownMenu viewControllerForColAtIndex:(NSInteger)index {
    ZYCPullDownMenuViewController *v = [self.viewControlls objectAtIndex:index];
    v.array = self.testArr.mutableCopy;
    return v;
}

// 返回下拉菜单多少列
- (NSInteger)numberOfColsInMenu:(PullDownMenuView *)pullDownMenu {
    return self.viewControlls.count;
}
- (CGFloat)pullDownMenu:(PullDownMenuView *)pullDownMenu heightForColAtIndex:(NSInteger)index {
    return self.testArr.count * 44;
}

#pragma mark - getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.menuView.frame)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCLectureRecordStatisticsCell class] forCellReuseIdentifier:NSStringFromClass([ZYCLectureRecordStatisticsCell class])];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (PullDownMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[PullDownMenuView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 46.0f)];
        _menuView.dataSource = self;
        _menuView.separateLineColor = [UIColor clearColor];
        _menuView.titleFont = kCustomFont(@"PingFangSC-Regular", 15.0f);
    }
    return _menuView;
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[self.currentSemesterName];
    }
    return _titleArr;
}


- (NSArray *)viewControlls {
    if (!_viewControlls) {
        _viewControlls = @[[NSClassFromString(@"ZYCPullDownMenuViewController") new]];
    }
    return _viewControlls;
}


- (NSArray *)testArr {
    if (!_testArr) {
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [documentPath stringByAppendingPathComponent:@"semester.data"];;
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        _testArr = array;
    }
    return _testArr;
}
- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
